package org.groupg.mockdatabyspring.morkdata.controller;


import lombok.extern.slf4j.Slf4j;
import org.groupg.mockdatabyspring.morkdata.model.DBDataFile;
import org.groupg.mockdatabyspring.morkdata.model.DBDataFiles;
import org.groupg.mockdatabyspring.morkdata.service.DefaultCreateService;
import org.groupg.mockdatabyspring.system.model.ResultBody;
import org.groupg.mockdatabyspring.system.model.enums.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * 根据请求创建文件
 */
@Slf4j
@Controller
@RequestMapping("dataFile")
public class DataFileCreateController {

    private final DefaultCreateService defaultCreateService;

    /**
     * 初始化对象的 Service
     * @param defaultCreateService 默认处理文件的方式
     */
    @Autowired
    public DataFileCreateController(DefaultCreateService defaultCreateService) {
        this.defaultCreateService = defaultCreateService;
    }


    /**
     * 根据前端发放的url创建文件内容
     *
     * @param dBDataFile 参见 {@link DBDataFile}
     * @return 请求状态对象，参见 {@link ResultBody}
     */
    @PostMapping("createOne")
    public @ResponseBody ResultBody createDBDataFile(@RequestBody DBDataFile dBDataFile) {

        var resultBody = new ResultBody(ResultStatus.OK, "完成创建");
        try {
            defaultCreateService.createFile(dBDataFile);
        } catch (IOException | InterruptedException | ExecutionException e) {
            log.error(e.getMessage(), e);
            resultBody = new ResultBody(ResultStatus.ERROR, e.getMessage());
        }
        return resultBody;
    }

    /**
     * 根据前端发放的url创建文件内容集合
     *
     * @param dBDataFiles 参见 {@link DBDataFiles}
     * @return 请求状态对象，参见 {@link ResultBody}
     */
    @PostMapping("createAny")
    public @ResponseBody ResultBody createDBDataFiles(@RequestBody DBDataFiles dBDataFiles) {

        var resultBody = new ResultBody(ResultStatus.OK, "完成创建");
//        try {
////            dBDataFiles.execute();
//        } catch (IOException | CheckException e) {
//            log.error(e.getMessage(), e);
//            resultBody = new ResultBody(ResultStatus.ERROR, e.getMessage());
//        }
        return resultBody;
    }
}
